package cate.game.activity.specall;

import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.specall.msg.SpeCallDataResp;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

@NoteClass("独立卡池召唤数据")
public class SpeCallActivity extends RoleActivitySingle {
    @NoteField("召唤")
    public SpeCall call;

    @NoteField("升星好礼")
    public SpeCallStarGift starGift;

    @NoteField("兑换")
    public SpeCallExchange exchange;

    @NoteField("海报")
    public SpeCallPoster poster;

    @NoteField("战令")
    public SpeCallWarOrder warOrder;

    @NoteField("独立卡池全局")
    public SpeCallGlobalData speCallGlobal;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (call == null) {
            call = new SpeCall();
        }
        call.initialize(this);
        if (starGift == null) {
            starGift = new SpeCallStarGift();
        }
        starGift.initialize(this);
        if (exchange == null) {
            exchange = new SpeCallExchange();
        }
        exchange.initialize(this);
        if (poster == null) {
            poster = new SpeCallPoster();
        }
        poster.initialize(this);
        if (warOrder == null) {
            warOrder = new SpeCallWarOrder();
        }
        warOrder.initialize(this);
        if (speCallGlobal == null) {
            speCallGlobal = new SpeCallGlobalData();
        }
        speCallGlobal.initialize(role);
    }

    public void noticeUpdate() {
        role.sendNow(new SpeCallDataResp(this));
    }

    @Override
    public void excelCheck() {
        starGift.excelCheck();
    }


    public void clear() {
        call.clear();
        exchange.clear();
        poster.clear();
        warOrder.clear();
    }

    /**
     * 活动开始时的处理
     */
    public void onStart() {
        call.onStart();
        starGift.onStart();
        exchange.onStart();
    }

    @Override
    public void onEnter() {
        super.onEnter();
        call.onEnter();
        starGift.onEnter();
        exchange.onEnter();
        poster.onEnter();
        warOrder.onEnter();
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        call.onDaySpanSafe(silence);
        starGift.onDaySpanSafe(silence);
        exchange.onDaySpanSafe(silence);
        poster.onDaySpanSafe(silence);
        warOrder.onDaySpanSafe(silence);
        if (!silence) {
            noticeUpdate();
        }
    }

    @Override
    public void onRoleOpen() {
        onStart();
    }

    @Override
    public void onRoleEnd() {
        clear();
    }

    @Override
    public void initSend() {
        role.sendNow(new SpeCallDataResp(this));
    }

    public GameResult<Void> starGiftAllowPay(PayItemRow payItemRow) {
        return starGift.allowPay(payItemRow);
    }

    public GameResult<Void> warOrderAllowPay(PayItemRow payItemRow) {
        return warOrder.allowPay(payItemRow);
    }

    @Override
    public void onPaySuccess(PayItemRow payItemRow) {
        starGift.onPaySuccess(payItemRow);
    }

    @JsonIgnore
    public GameResult<SpeCallActivity> getReward(int tid) {
        GameResult<SpeCallActivity> r = new GameResult<>();
        r.data = this;
        EcResult<Void> gr = starGift.getReward(tid);
        if (!r.ok()) {
            r.fail(gr);
        }
        return r;
    }

    public GameResult<SpeCallActivity> exchange(int tid, int num) {
        GameResult<SpeCallActivity> r = new GameResult<>();
        r.data = this;
        EcResult<Void> er = exchange.exchangeItem(tid, num);
        if (!r.ok()) {
            r.fail(er);
        }
        return r;
    }

    public GameResult<SpeCallActivity> callExe(int type) {
        GameResult<SpeCallActivity> r = new GameResult<>();
        r.data = this;
        GameResult<Void> er = call.call(type);
        if (!er.ok()) {
            r.fail(er);
        }
        return r;
    }

    public GameResult<SpeCallActivity> posterGetReward() {
        GameResult<SpeCallActivity> r = new GameResult<>();
        r.data = this;
        EcResult<Void> er = poster.getBigPrize();
        if (!r.ok()) {
            r.fail(er);
        }
        return r;
    }

    public void updateAvailable(String uid) {
        starGift.updateAvailable(uid);
    }

    public GameResult<SpeCallActivity> warOrderGetReward(int tid) {
        GameResult<SpeCallActivity> r = new GameResult<>();
        r.data = this;
        EcResult<Void> er = warOrder.onGetReward(tid);
        if (!r.ok()) {
            r.fail(er);
        }
        return r;
    }

    public void warOrderAddNum(int heroTid, int num) {
        warOrder.addNum(heroTid, num);
    }

    public void addNums(List<Integer> heroTids) {
        if (CollectionUtils.isEmpty(heroTids)) {
            return;
        }
        heroTids.forEach(c -> warOrderAddNum(c, 1));
    }
}
